<?php
/**
 * 用户管理
 * @author zelome@163.com
 * $Id: index.php 10 2011-02-23 07:01:48Z sbnnest@gmail.com $
 */

require_once('../common.inc.php');


class userlist extends Action
{
	
	/**
	 * 获取用户列表
	 */
	function doDefault()
	{
		$page = $this->app->page();
		$page->params['template'] = 'content/userlist.tpl';
		
		
		$db_r = $this->app->orm()->query();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addField('user_name');
		$db_r->addField('user_nickname');
		$db_r->addField('user_sex');
		$db_r->addField('user_password');
		$db_r->addField('user_create_time');
		$rs = $db_r->getArray(NULL,1,20);
		$page->value('userlist',$rs);
		$page->output();
	}
	
	
	/**
	 * 删除用户
	 */
	function doDelete(){
		
		$userid = $_GET['userid'];
		$db_r = $this->app->orm()->query();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addWhere('user_id',$userid);
		$db_r->delete();
		
		
		$page = $this->app->page();
		$page->params['template'] = 'content/userlist.tpl';
		
		
		$db_r = $this->app->orm()->query();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addField('user_name');
		$db_r->addField('user_password');
		$db_r->addField('user_nickname');
		$db_r->addField('user_sex');
		$db_r->addField('user_create_time');
		$rs = $db_r->getArray(NULL,14,2);
		$page->value('userlist',$rs);
		
		$page->output();
	}
	
	/**
	 * 跳转到添加用户页面
	 */
	function doAddpage()
	{
		$page = $this->app->page();
		$page->params['template'] = 'content/useradd.tpl';
		$page->output();
	}
	
	/**
	 * 添加用户
	 */
	function doAdd() {
		
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$nickname = trim($_POST['nickname']);
		$sex = trim($_POST['sex']);
		$page = $this->app->page();
		$db_r = $this->app->orm()->query();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addField('user_name');
		$db_r->addField('user_password');
		$db_r->addField('user_create_time');
		array(array('id'=>1));
		$db_r->insert(array('user_sex'=>sex,'user_nickname'=> $nickname, 'user_name' => $username,'user_password' => $password,'user_create_time'=> date('Y-m-d H:i:s')));
		
		$this->app->redirect('contentframe.php?do=userlist');
	}
	
	/**
	 * 跳转到修改用户页面
	 */
	function doModifypage() {
		$page = $this->app->page ();
		$page->params ['template'] = 'content/usermodify.tpl';
		
		$user_id = trim ( $_GET ['userid'] );
		$page = $this->app->page ();
		$db_r = $this->app->orm ()->query ();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addField('user_name');
		$db_r->addField('user_password');
		$db_r->addField('user_nickname');
		$db_r->addField('user_sex');
		$db_r->addField('user_create_time');
		$db_r->addWhere ( 'user_id', $user_id );
		$result = $db_r->getRow ();
		$page->value ( 'user_id', $user_id );
		$page->value ( 'username', $result ['user_name'] );
		$page->value ( 'password', $result ['user_password'] );
		$page->value ( 'nickname', $result ['user_nickname'] );
		$page->value ( 'sex', $result ['user_sex'] );
		$page->output ();
	}
	
	/**
	 * 修改用户
	 */
	function doModify() {
		
		$page = $this->app->page ();
		$page->params ['template'] = 'content/usermodify.tpl';
		
		$user_id = trim ( $_POST ['user_id'] );
		$username = trim ( $_POST ['username'] );
		$password = trim ( $_POST ['password'] );
		$nickname = trim($_POST['nickname']);
		$usersex = trim($_POST['usersex']);
		$page = $this->app->page ();
		$db_r = $this->app->orm ()->query ();
		$db_r->addTable('tb_user');
		$db_r->addField('user_id');
		$db_r->addField('user_name');
		$db_r->addField('user_password');
		$db_r->addField('user_nickname');
		$db_r->addField('user_sex');
		$db_r->addField('user_create_time');
		$db_r->addWhere ( 'user_id', $user_id );
		$db_r->update ( array ('user_nickname' => $nickname,'user_sex'=>$usersex,'user_name' => $username, 'user_password' => $password ) );
		
		$page->output ();
	}
}
											
$app->run();

?>
